<?php

namespace app\common\service;

use think\Exception;

/**
 * 金额计算
 */
class Amount{

    /**
     * 获取广告置顶费用
     */
    public static function getAdTopFee(int $days = 0){
        $conf = config('site.toptime');
        return self::check($days, $conf);
    }

    /**
     * 获取广告有效时间费用
     */
    public static function getAdExpFee(int $days = 0){
        $conf = config('site.effecttime');
        return self::check($days, $conf);
    }

    /**
     * 获取广告推荐费用
     */
    public static function getAdRecFee(int $days = 0){
        $conf = config('site.recommendtime');
        return self::check($days, $conf);
    }

    /**
     * 获取赏金广告置服务费用
     * @param string $amount 赏金总额
     */
    public static function getAdSerFee($amount = 0){
        $conf = config('site.service_fee');
        return bcdiv(bcmul($conf , $amount, 2), 100, 2);
    }

    /**
     * 获取广告应付总费用
     * @param string $amount 赏金总额
     */
    public static function getAdTotalFee($data = []){
        //计算支付总金额 = 赏金总额(服务费已经包含) + 置顶 + 推荐 + 有效时间费用
        $amount = 0;

        if(isset($data['money_amount'])){
            $amount = bcadd($amount, $data['money_amount'], 2);
        }

        if(isset($data['top_fee'])){
            $amount = bcadd($amount, $data['top_fee'], 2);
        }

        if(isset($data['recom_fee'])){
            $amount = bcadd($amount, $data['recom_fee'], 2);
        }

        if(isset($data['exp_fee'])){
            $amount = bcadd($amount, $data['exp_fee'], 2);
        }

        // 服务费已经包含在赏金费用中
        // if(isset($data['service_fee'])){
        //     $amount = bcadd($amount, $data['service_fee'], 2);
        // }

        return $amount;
    }
    protected static function check(int $days, array $conf = []){
        if($days && $conf){
            if(isset($conf[$days])){
                return $conf[$days];
            }
            $json = json_encode($conf);
            throw new Exception("时间参数错误{$days}--json--{$json}");
        }
        return 0;
    }

}